package com.mx.zb.mng.ctrl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import com.mx.zb.mng.bo.Agent;
import com.mx.zb.mng.bo.BizIndustry;
import com.mx.zb.mng.bo.Note;
import com.mx.zb.mng.bo.Template;
import com.mx.zb.mng.bo.Tenant;
import com.mx.zb.mng.dao.AgentMapper;
import com.mx.zb.mng.dao.BizIndustryMapper;
import com.mx.zb.mng.dao.NoteMapper;
import com.mx.zb.mng.dao.TemplateMapper;
import com.mx.zb.mng.dao.TenantMapper;
import com.mx.zb.mng.utils.UnionCode;
import com.mx.zb.mng.utils.context.ContextThread;
import com.mx.zb.mng.utils.db2.Page;
import com.mx.zb.mng.utils.db2.PageHelper;
import com.mx.zb.mng.utils.ex.PageAnnotation;
import com.mx.zb.mng.utils.ex.ServiceException;
import net.minidev.json.JSONObject;

/***
 * 
 * @author zd
 *
 */

@Controller
@PageAnnotation
@RequestMapping("/p/note")
public class NoteCtrl {

	@Autowired
	NoteMapper noteMapper;
	@Autowired
	AgentMapper agentMapper;
	@Autowired
	TemplateMapper templateMapper;
    @Autowired
   BizIndustryMapper  bizIndustryMapper;
	@RequestMapping(value = { "/mng" })
	public String toMng(Model model) {
		List<BizIndustry> tmpList = bizIndustryMapper.selectTemplates();
		model.addAttribute("tmpList", tmpList);
		// 判断当前用户是分销，代理商还是总部
		Agent t = agentMapper.selectByPrimaryKey(ContextThread.get().getAgentId());
		model.addAttribute("agent", t);
		return "addNote";
	}

	@RequestMapping(value = { "/reader" })
	public String toNote(Model model) {
		Note no = new Note();
		// 总部，分销代理商，代理商查看自己添加的公告
		no.setAgentId(ContextThread.get().getAgentId());
		PageHelper.startPage();
		Page<Note> artList = noteMapper.queryPage(no);
		model.addAttribute("noteList", artList);
		List<BizIndustry> tmpList = bizIndustryMapper.selectTemplates();
		model.addAttribute("tpList", tmpList);
		return "noteLists";
	}

	@RequestMapping(value = { "/reader/show" })
	public String showNote(@RequestParam String id, Model model) {
		Note o = noteMapper.selectByPrimaryKey(id);
		List<BizIndustry> tmpList = bizIndustryMapper.selectTemplates();
		model.addAttribute("tmpList", tmpList);
		model.addAttribute("note", o);
		//查一下是否是总部
		boolean isZongBu = false;
				Agent a = agentMapper.selectByPrimaryKey(ContextThread.get().getAgentId());
				 if(a!=null) {
					if("总部".equals(a.getType())) {
						isZongBu = true;
					}else {
						isZongBu = false;
					}
				 }
		model.addAttribute("isZongBu",isZongBu);	 
		return "noteEdit";
	}

	@RequestMapping(value = { "/reader/toReader" })
	public String toaddNote(Model model) {
		List<BizIndustry> tmpList = bizIndustryMapper.selectTemplates();
		model.addAttribute("tmpList", tmpList);
		// 判断当前用户是分销，代理商还是总部
		Agent t = agentMapper.selectByPrimaryKey(ContextThread.get().getAgentId());
		model.addAttribute("agent", t);
		return "addNote";
	}

	@RequestMapping(value = { "/reader/addReader" })
	@Transactional
	public String addNote(Note o, Model model) {
		String nid = UnionCode.getShortUuid();
		o.setId(nid);
		o.setCreateDate(new Date());
		o.setUpdateDate(o.getCreateDate());
		o.setCreateOper(ContextThread.get().getOperatorId());
		o.setUpdateOper(ContextThread.get().getOperatorId());
		o.setAgentId(ContextThread.get().getAgentId());
		noteMapper.insert(o);
		Note ot = noteMapper.selectByPrimaryKey(nid);
		model.addAttribute("note", ot);
		// return "noteEdit";
		Note no = new Note();
		no.setAgentId(ContextThread.get().getAgentId());
		PageHelper.startPage();
		Page<Note> artList = noteMapper.queryPage(no);
		model.addAttribute("noteList", artList);
		return "redirect:/p/note/reader";
	}

	@RequestMapping(value = { "/reader/editReader" })
	@Transactional
	public String editNote(Note o, Model model) {
		o.setUpdateDate(o.getCreateDate());
		o.setUpdateOper(ContextThread.get().getOperatorId());
		noteMapper.updateByPrimaryKeySelective(o);
		model.addAttribute("note", o);
		List<BizIndustry> tmpList = bizIndustryMapper.selectTemplates();
		model.addAttribute("tmpList", tmpList);
		// return "noteEdit";
		return "redirect:/p/note/reader";
	}

}
